package
{
	import flash.utils.Dictionary;
	
	import org.flixel.FlxGroup;
	import org.flixel.FlxObject;
	import org.flixel.FlxRect;
	import org.flixel.FlxSprite;
	import org.flixel.FlxTilemap;
	import org.flixel.system.FlxTile;

	public class Zone extends FlxGroup
	{
		public var walkableTiles:FlxTilemap = new FlxTilemap();
		public var shadowHumanObjects:FlxTilemap = new FlxTilemap();
		public var shadowObjects:FlxTilemap = new FlxTilemap();
		public var humanObjects:FlxTilemap = new FlxTilemap();
		public var doorTiles:FlxTilemap = new FlxTilemap();
		public var doors:FlxGroup = new FlxGroup();
		public var width:int;
		public var height:int;
		protected var doorsToSet:Dictionary = new Dictionary();
		
		public function Zone()
		{
			
		}
		public function SetDoorsBoundingBoxes():void
		{
			var boundingBoxes:Dictionary = new Dictionary();
			for(var k:Object in doorsToSet)
			{
				boundingBoxes[int(k)] = new FlxRect(0,0,0,0);
			}
			
			for(var y:int=0;y<doorTiles.heightInTiles;y++)
			{
				for(var x:int=0;x<doorTiles.widthInTiles;x++)
				{
					var doorColor:int = doorTiles.getTile(x,y);
					if( doorColor != 0)
					{
						//If no information have been stored about the boundingbox save the data as a 16x16 tile
						if(boundingBoxes[doorColor].width == 0)
						{
							boundingBoxes[doorColor].x = x;
							boundingBoxes[doorColor].y = y;
							boundingBoxes[doorColor].width = 16;
							boundingBoxes[doorColor].height = 16;
						}
						//Make the bounding box wider if the last tile was of same color
						else if(x > 0 && doorTiles.getTile(x-1,y) == doorColor)
						{
							boundingBoxes[doorColor].width +=16;
						}
						//Make the bounding box higher if the last tile was of same color
						else if(y > 0 && doorTiles.getTile(x,y-1) == doorColor)
						{
							boundingBoxes[doorColor].height +=16;
						}
					}
				}
			}
			for(var j:Object in boundingBoxes)
			{
				var key:int = int(j);
				doors.add(new Door(this,doorsToSet[key],key,boundingBoxes[key]));
			}
		}
	}
}